import React, { useState,useEffect,useReducer } from 'react';
import Child from './np.jsx';

let initialCount = 0;
const P = {
    init(initialCount) {
        return {count: initialCount};
    },
    reducer(state, action) {
        switch (action.type) {
            case 'increment':
                return {count: state.count + 1};
            case 'decrement':
                return {count: state.count - 1};
            case 'reset':
                return P.init(action.payload);
            default:
                throw new Error();
        }
    }
}


const View = ()=>{

    const [state, dispatch] = useReducer(P.reducer, initialCount, P.init);
    return(
        <div>
            Count: {state.count}
            <button
            onClick={() => dispatch({type: 'reset', payload: initialCount})}>
            Reset
            </button>
            <button onClick={() => dispatch({type: 'decrement'})}>-</button>
            <button onClick={() => dispatch({type: 'increment'})}>+</button>
            <div className="m">
                <h3>这是useReducer</h3>
                <Child/>
            </div>
        </div>
    )
}
export default View;
